import axios from "axios";
import store from '@/store/index'
import { Message } from 'element-ui'
//创建一个新的axios实例
const request = axios.create({
    baseURL: 'http://localhost:8095/',
    timeout: 5000,
    withCredentials: true
  })

//添加响应拦截器
request.interceptors.response.use(function (response){
    //对响应数据做些什么
    if (response.data.code == 1&&response.data.msg == "认证失败"){
        store.commit("user/del")
        window.top.location.href = '/'
    }
    if (response.data.code == 1){
        Message.error("操作失败:"+response.data.msg)
    }
    return response
}, function (error){
    //对响应错误做些什么
    return Promise.reject(error)
})


// 添加请求拦截器
request.interceptors.request.use(function (config) {
    // 在发送请求之前做些什么
    const { token } = store.state.user
    if (token) {
        config.headers.token = token
    }
    return config
}, function (error) {
    // 对请求错误做些什么
    return Promise.reject(error)
})

export default request